package shiroTest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;

public class Demo01_Shiro {


    /**
     * 未使用自定义Realm
     */
    @Test
    public void test1(){

        // 1.获取SecurityManager工厂
        Factory<SecurityManager> factory  = new IniSecurityManagerFactory("classpath:shiro.ini");

        // 2.获取SecurityManager 对象
        SecurityManager securityManager = factory.getInstance();

        // 3.将SecurityManager绑定到当前项目运行环境中
        SecurityUtils.setSecurityManager(securityManager);

        // 4. 获取当前用户的主体信息(当前用户并没有登录)
        Subject currUser = SecurityUtils.getSubject();

        // 5. 创建票据,输入用户名密码
        UsernamePasswordToken token=new UsernamePasswordToken("zhangsan","123456");

        try {
            currUser.login(token);
        } catch (UnknownAccountException e) {
            // 用户名不存在抛出此异常
            e.printStackTrace();
        } catch (IncorrectCredentialsException e){

            // 密码错误抛出此异常
            e.printStackTrace();
        }

        System.out.println("是否登录成功: "+currUser.isAuthenticated());

        // 退出登录
        currUser.logout();

        System.out.println("是否登录成功: "+currUser.isAuthenticated());
    }


    /**
     * 使用自定义Realm
     */
    @Test
    public void test2(){
        // 1.获取SecurityManager工厂
        Factory<SecurityManager> factory  = new IniSecurityManagerFactory("classpath:shiro-realm.ini");

        // 2.获取SecurityManager 对象
        SecurityManager securityManager = factory.getInstance();

        // 3.将SecurityManager绑定到当前项目运行环境中
        SecurityUtils.setSecurityManager(securityManager);

        // 4. 获取当前用户的主体信息(当前用户并没有登录)
        Subject currUser = SecurityUtils.getSubject();

        // 5. 创建票据,输入用户名密码
        UsernamePasswordToken token=new UsernamePasswordToken("zhangsan","12333");

        try {
            currUser.login(token);
        } catch (UnknownAccountException e) {
            // 用户名不存在抛出此异常
            e.printStackTrace();
        } catch (IncorrectCredentialsException e){

            // 密码错误抛出此异常
            e.printStackTrace();
        }

        System.out.println("是否登录成功: "+currUser.isAuthenticated());

        // 退出登录
        currUser.logout();

        System.out.println("是否登录成功: "+currUser.isAuthenticated());
    }

    /**
     * shiro加密
     */
    @Test
    public void test3() {

        String password = "123";

        // 传统的md5加密
        Md5Hash md5 = new Md5Hash(password);

        // 202cb962ac59075b964b07152d234b70
        System.out.println(md5);

        // md5 + 盐
        md5 = new Md5Hash(password, "123");

        // 4297f44b13955235245b2497399d7a93
        System.out.println(md5);

        // md5 + 盐 + 散列次数
        md5 = new Md5Hash(password, "123", 3);

        // 8eda1535b635e6b2c3672cc502249fe0
        System.out.println(md5);

    }

    /**
     * 加密Realm
     */
    @Test
    public void test4() {

        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-crypt.ini");

        SecurityManager securityManager = factory.getInstance();

        SecurityUtils.setSecurityManager(securityManager);

        // 获取主体
        Subject currUser = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "123");

        try {
            currUser.login(token);
        } catch (UnknownAccountException e) {
            e.printStackTrace();
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
        }


        System.out.println("是否登录成功: " + currUser.isAuthenticated());

        currUser.logout();

        System.out.println("是否登录成功: " + currUser.isAuthenticated());
    }

    /**
     * 授权
     */
    @Test
    public void test5() {

        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-authorization.ini");

        SecurityManager securityManager = factory.getInstance();

        SecurityUtils.setSecurityManager(securityManager);

        // 获取主体
        Subject currUser = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "111");

        try {
            currUser.login(token);
        } catch (UnknownAccountException e) {
            e.printStackTrace();
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
        }

        if(currUser.hasRole("r1")){
            System.out.println("当前登录用户具备r1角色");
        }

        if(currUser.isPermitted("user:create")){
            System.out.println("当前登录用户具备对user资源的create权限");
        }

        // 退出登录
        currUser.logout();

    }
}